//
//  ZYPresentationController.swift
//  SwiftDemo1
//
//  Created by wangzhipan on 2025/6/3.
//

import Foundation
import UIKit


// 重写UIPresentationController 的 containerViewWillLayoutSubviews方法，设置presentedView的尺寸
class ZYPresentationController: UIPresentationController {
    
    var presentedViewFrame: CGRect = CGRect.zero
    
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        
        presentedView?.frame = presentedViewFrame
        addCoverView()
    }
    
    private func addCoverView() {
        
        let view = UIView(frame: self.containerView?.bounds ?? CGRect.zero)
        view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25)
        containerView?.addSubview(view)
        containerView?.sendSubviewToBack(view)
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapped))
        view.addGestureRecognizer(tapGesture)
    }
    
    @objc private func tapped() {
        presentedViewController.dismiss(animated: true)
    }
    
}
